package com.hanbang.chu_xing_gao_zhao.view_me;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.base.MainBaseFragmentActivity;
import com.hanbang.chu_xing_gao_zhao.myview.NoScrollViewPager;
import com.hanbang.cxgz_2.R;

import java.util.ArrayList;

/**
 * 我的收藏
 */
public class MeCollectFragmentActivity extends MainBaseFragmentActivity {
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private NoScrollViewPager viewPager;
    private FragmentActivityAdapter adapter;

    private ArrayList<TextView> tvList = new ArrayList<TextView>();
    private TextView esoterica;
    private TextView data;
    private TextView classroom;
    private TextView stadard;
    private TextView infor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.me_collect_fragment_activity);
        initView();
        setOnClickListener();
    }

    /**
     * 初始化view
     */
    private void initView() {
        setTitleMore(View.VISIBLE, View.INVISIBLE, "我的收藏", null, null, null);
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));
        viewPager = (NoScrollViewPager) findViewById(R.id.vp_me_collet_fragment_activity_viewpage);

        esoterica = (TextView) findViewById(R.id.tv_me_collect_fragment_activity_collect);
        data = (TextView) findViewById(R.id.tv_me_collect_fragment_activity_data);
        classroom = (TextView) findViewById(R.id.tv_me_collect_fragment_activity_classroom);
        stadard = (TextView) findViewById(R.id.tv_me_collect_fragment_activity_stadard);
        infor = (TextView) findViewById(R.id.tv_me_collect_fragment_activity_infor);

        esoterica.setText("烹饪\n秘籍");
        data.setText("学习\n资料");
        classroom.setText("学习\n课堂");
        stadard.setText("餐饮\n标准化");
        infor.setText("信息");

        tvList.add(esoterica);
        tvList.add(data);
        tvList.add(classroom);
        tvList.add(stadard);
        tvList.add(infor);


        fragments.add(new MeCollectEstericaFragment());
        fragments.add(new MeCollectDataFragment());
        fragments.add(new MeCollectClassroomFragment());
        fragments.add(new MeCollectStandardFragment());
        fragments.add(new MeCollectInforFragment());

        adapter = new FragmentActivityAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        setColor(0);
    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        esoterica.setOnClickListener(onClickListener);
        data.setOnClickListener(onClickListener);
        classroom.setOnClickListener(onClickListener);
        stadard.setOnClickListener(onClickListener);
        infor.setOnClickListener(onClickListener);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    //烹饪秘籍
                    case 0:
                        setColor(0);
                        viewPager.setCurrentItem(0);
                        break;
                    //学习资料
                    case 1:
                        setColor(1);
                        viewPager.setCurrentItem(1);
                        break;
                    //学习课堂
                    case 2:
                        setColor(2);
                        viewPager.setCurrentItem(2);
                        break;
                    //餐饮标准化
                    case 3:
                        setColor(3);
                        viewPager.setCurrentItem(3);
                        break;
                    //信息
                    case 4:
                        setColor(4);
                        viewPager.setCurrentItem(4);
                        break;


                    default:

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    /**
     * 处理监听
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                default:
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }


    /**
     * viewPage 适配器
     */
    public class FragmentActivityAdapter extends FragmentStatePagerAdapter {
        private ArrayList<android.support.v4.app.Fragment> fragments;
        FragmentManager fm;

        public FragmentActivityAdapter(FragmentManager fm, ArrayList<android.support.v4.app.Fragment> list) {
            super(fm);
            if (fragments == null) {

                this.fragments = list;
                this.fm = fm;

            } else {
                this.fragments.clear();
                fragments = new ArrayList<android.support.v4.app.Fragment>();
            }
        }


        @Override
        public android.support.v4.app.Fragment getItem(int position) {

            return fragments.get(position);
        }

        @Override
        public int getCount() {

            return fragments.size();
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return super.isViewFromObject(view, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);

        }
    }


    /**
     * 更改标题背景颜色
     *
     * @param
     */
    public void setColor(int id) {
        for (int i = 0; i < tvList.size(); i++) {
            if (id == i) {
                tvList.get(i).setTextColor(0xffFFD30F);
            } else {
                tvList.get(i).setTextColor(0xff000000);
            }
        }

    }

}
